package com.csw.shuanfa.utils.id;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisIDGenerator {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 最近做压力测试，并发只弄到了 500 就报错了：org.springframework.data.redis.RedisSystemException: Unknown redis exception; nested exception is java.util.concurrent.RejectedExecutionException: Thread limit exceeded replacing blocked worker
     * <p>
     * springboot 中使用 redis 并发 500 时线程错误-鸿蒙开发者社区-51CTO.COM  https://ost.51cto.com/answer/6093
     * 一定要自定义线程池
     *
     * @return
     */
    public long generateId() {
        Long increment = stringRedisTemplate.opsForValue().increment("id-generateId");
        String res = String.format("%06d", increment);
        res = res.substring(res.length() - 6);
        return Long.valueOf(System.currentTimeMillis() + res);
    }

}

